//https://leetcode.cn/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/s
class Solution {
public:
    ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
        int i = 0;
        int n = 0;
        ListNode* curA = headA;
        ListNode* curB = headB;

        while (curA)
        {
            curA = curA->next;
            ++i;
        }

        while (curB)
        {
            curB = curB->next;
            ++n;
        }
        if (i == n && headA == headB)
        {
            return headA;
        }

        ListNode* max = i > n ? headA : headB;
        ListNode* min = i > n ? headB : headA;
        for (int k = abs(i - n); k > 0; --k)
        {
            max = max->next;
        }

        while (max && min)
        {
            if (max == min)
            {
                return max;
            }
            max = max->next;
            min = min->next;
        }

        return nullptr;
    }
};